/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-7-11 上午10:21:25
 */
package com.absir.android.orm;

import java.util.HashMap;
import java.util.Map;

import android.database.Cursor;

import com.absir.bean.core.BeanFactoryUtils;

/**
 * @author absir
 * 
 */
public class DaoCursorUtils {

	/** FACTORY */
	public static final DaoFactory FACTORY = BeanFactoryUtils.get(DaoFactory.class);

	/**
	 * @param cursor
	 * @return
	 */
	public static String[] getCulomnNames(Cursor cursor) {
		int count = cursor.getColumnCount();
		String[] culomnNames = new String[count];
		for (int i = 0; i < count; i++) {
			culomnNames[i] = cursor.getColumnName(i);
		}

		return culomnNames;
	}

	/**
	 * @param cursor
	 * @return
	 */
	public static String[] getResultColumns(Cursor cursor) {
		int count = cursor.getColumnCount();
		String[] resultCulomns = new String[count];
		for (int i = 0; i < count; i++) {
			resultCulomns[i] = cursor.getString(i);
		}

		return resultCulomns;
	}

	/**
	 * @param cursor
	 * @return
	 */
	public static Map<String, String> getOneRow(Cursor cursor) {
		return getOneRow(cursor, null);
	}

	/**
	 * @param cursor
	 * @param map
	 * @return
	 */
	public static Map<String, String> getOneRow(Cursor cursor, Map<String, String> map) {
		if (cursor.moveToNext()) {
			if (map == null) {
				map = new HashMap<String, String>();
			}

			int count = cursor.getColumnCount();
			for (int i = 0; i < count; i++) {
				map.put(cursor.getColumnName(i), cursor.getString(i));
			}

			return map;
		}

		return null;
	}

}
